package antlr.calc;


public class MyCalcVisitor extends CalcBaseVisitor<Integer> {
    public int calcCnt = 0;

    @Override
    public Integer visitNumber(CalcParser.NumberContext ctx) {
        return Integer.valueOf(ctx.NUMBER().getText());
    }

    @Override
    public Integer visitCalc(CalcParser.CalcContext ctx) {
        calcCnt++;
        Integer l = visit(ctx.expr(0));
        Integer r = visit(ctx.expr(1));
        CalcParser.OpContext op = ctx.op();
        if (op.ADD() != null) {
            return l + r;
        } else if (op.SUB() != null) {
            return l - r;
        } else if (op.MUL() != null) {
            return l * r;
        } else if (op.DIV() != null) {
            return l / r;
        }
        return 0;
    }

    @Override
    public Integer visitBrackets(CalcParser.BracketsContext ctx) {
        Integer visit = visit(ctx.expr());
        return visit;
    }
}
